﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace IdentityData
{
    internal class MyEntityStore<T> where T : class
    {
        public MyEntityStore(DbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            Context = context;
            DbEntitySet = context.Set<T>();
        }

        public DbContext Context { get; private set; }

        public IQueryable<T> EntitySet
        {
            get { return DbEntitySet; }
        }

        public DbSet<T> DbEntitySet { get; private set; }

        public virtual Task<T> GetByIdAsync(object id)
        {
            return DbEntitySet.FindAsync(new object[1]
            {
                id
            });
        }

        public void Create(T entity)
        {
            DbEntitySet.Add(entity);
        }

        public void Delete(T entity)
        {
            DbEntitySet.Remove(entity);
        }
    }
}